package com.cpecc.controller.wuzi;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.cpecc.manager.system.UserManager;
import com.cpecc.manager.wuzi.BaoFeiManager;
import com.cpecc.manager.wuzi.BaoFeiSPRManager;
import com.cpecc.manager.wuzi.BuPeiManager;
import com.cpecc.manager.wuzi.CaiGouSPRManager;
import com.cpecc.manager.wuzi.HuanHuoManager;
import com.cpecc.manager.wuzi.ShenQingManager;
import com.cpecc.manager.wuzi.TuiHuoManager;
import com.cpecc.manager.wuzi.WeiXiuManager;
import com.cpecc.manager.wuzi.WuZiManager;
import com.cpecc.manager.wuzi.YiJiaoManager;
import com.cpecc.model.system.User;

@Controller
@RequestMapping(value = "/todo")
public class ToDoController {
	
	@Autowired
	private UserManager userManager;
	
	@Autowired
	private CaiGouSPRManager caiGouSPRManager;
	
	@Autowired
	private ShenQingManager shenQingManager;
	
	@Autowired
	private TuiHuoManager tuiHuoManager;
	
	@Autowired
	private HuanHuoManager huanHuoManager;
	
	@Autowired
	private WeiXiuManager weiXiuManager;
	
	@Autowired
	private YiJiaoManager yiJiaoManager;
	
	@Autowired
	private BaoFeiSPRManager baoFeiSPRManager;
	
	@Autowired
	private BaoFeiManager baoFeiManager;
	
	@Autowired
	private BuPeiManager buPeiManager;
	
	@Autowired
	private WuZiManager wuZiManager;
	
	@RequestMapping(value = "/getTotalNum")
	public void getTotalNum(HttpServletResponse response){		
		int zongShu=0;
		Subject currentUser = SecurityUtils.getSubject();
		Object obj=currentUser.getPrincipals();
		String name=obj.toString();
		User user=userManager.findByUsername(name);
		//采购数
		if(currentUser.hasRole("部门负责人"))
			zongShu+=caiGouSPRManager.getMySPList(user).size();		
		if(currentUser.hasRole("固资管理员")){
			zongShu+=shenQingManager.getAllCaiGouZT(111).size();
			zongShu+=shenQingManager.getAllYanShouRK().size();
			zongShu+=shenQingManager.getAllCaiGouZT(106).size();
			zongShu+=shenQingManager.getAllCaiGouZT(108).size();
			zongShu+=tuiHuoManager.getAllTuiHuoSQ(303).size();
		}
		if(currentUser.hasRole("财务")){
			zongShu+=shenQingManager.getAllCaiGouZT(109).size();
		}
		zongShu+=shenQingManager.getAllCaiGouZT2CMR(103,user).size();
		zongShu+=huanHuoManager.getAllHuanHuoSQ2CMR(1,user).size();
		zongShu+=tuiHuoManager.getAllTuiHuoSQ2CMR(302,user).size();		
		zongShu+=shenQingManager.getAllLingYong(user).size();
		
		//维修数
		if(currentUser.hasRole("固资管理员")){
			zongShu+=weiXiuManager.getAllWeiXiuByZT(402).size();
			zongShu+=weiXiuManager.getAllWeiXiuByZT(405).size();
			zongShu+=weiXiuManager.getAllWeiXiuByZT(204).size();
		}
		zongShu+=huanHuoManager.getAllHuanHuoSQ2CMR(2, user).size();
		zongShu+=weiXiuManager.getMyWeiXiuQR(user).size();
		zongShu+=weiXiuManager.getMyWeiXiuRW(user).size();
		
		//移交数
		if(currentUser.hasRole("固资管理员")){
			zongShu+=yiJiaoManager.getAllYiJiao(502).size();
			zongShu+=yiJiaoManager.getAllYiJiao(503).size();
			zongShu+=yiJiaoManager.getAllYiJiao(505).size();
		}
		zongShu+=yiJiaoManager.getMyYiJiaoJS(user).size();
		
		//退回数
		if(currentUser.hasRole("固资管理员"))
			zongShu+=yiJiaoManager.getAllTuiHui().size();
		
		//报废数
		if(currentUser.hasRole("部门负责人"))
			zongShu+=baoFeiSPRManager.getMyBMSPList(user).size();
		if(currentUser.hasRole("固资管理员")){
			zongShu+=baoFeiManager.getBaoFeiList(703).size();
			zongShu+=baoFeiManager.getBaoFeiList(706).size();
			zongShu+=baoFeiManager.getBaoFeiList(708).size();
		}
		if(currentUser.hasRole("财务")){
			zongShu+=baoFeiManager.getBaoFeiList(707, user).size();
		}
		zongShu+=baoFeiManager.getBaoFeiNeiGouList(712,user).size();
		zongShu+=baoFeiSPRManager.getMySPList(user).size();
		
		//盘点数
		if(currentUser.hasRole("固资管理员")){
			zongShu+=buPeiManager.getBuPeiList(802).size();
			zongShu+=buPeiManager.getBuPeiList(805).size();
		}
		if(currentUser.hasRole("财务")){
			zongShu+=buPeiManager.getBuPeiList(804).size();
		}
		zongShu+=wuZiManager.getMyPanDian(user).size();
		zongShu+=buPeiManager.getMyBuPeiList(user, 803).size();
		
		PrintWriter out = null;
		response.setContentType("application/json;charset=utf-8");
		String result="{\"numOfToDo\":\""+zongShu+"\"}";
		try {
			out = response.getWriter();
			out.write(result);
		} catch (Exception e) {
		}	
	}
}
